package com.xintonghua.bussiness.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xintonghua.base.widget.MyItemTextView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.bean.AddLogItemBean;
import com.xintonghua.bussiness.ui.home.AddLogActivity;
import com.xintonghua.bussiness.ui.home.AddProductActivity;
import com.xintonghua.bussiness.ui.home.AddProjectActivity;
import com.xintonghua.bussiness.ui.home.AddServicePersonActivity;
import com.xintonghua.bussiness.util.MyState;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by dell on 2017/5/9.
 */

public class AddLogAdapter extends BaseAdapter {
    private AddLogActivity activity;
    private List<AddLogItemBean> list;
    private int mCurClickPosition;
    private boolean click = true;

    public AddLogAdapter(AddLogActivity activity, List<AddLogItemBean> list) {
        this.activity = activity;
        this.list = list;
    }

    public boolean isClick() {
        return click;
    }

    public void setClick(boolean click) {
        this.click = click;
    }

    public int getCurPosition() {
        return mCurClickPosition;
    }

    public void setCurPosition(int mCurClickPosition) {
        this.mCurClickPosition = mCurClickPosition;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public AddLogItemBean getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public List<AddLogItemBean> getList() {
        return list;
    }

    public void setList(List<AddLogItemBean> list) {
        this.list = list;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.adapter_add_log, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        AddLogItemBean bean = list.get(position);
        holder.mUtilsProject.setMsg(bean.getProject());
        holder.mUtilsPerson.setMsg(bean.getPerson());
        if (bean.getmCurUseProduct().size() == 0) {
            holder.mTvNeed.setVisibility(View.INVISIBLE);
        } else {
            holder.mTvNeed.setVisibility(View.VISIBLE);
        }
        holder.mTvReduceItem.setVisibility(isClick() ? View.VISIBLE : View.GONE);
        holder.mUtilsProject.setEnabled(isClick() ? true : false);
        holder.mUtilsPerson.setEnabled(isClick() ? true : false);
        holder.mTvChangeNeed.setVisibility(isClick() ? View.VISIBLE : View.GONE);

        holder.mTvNeed.setAdapter(new TagAdapter<AddLogItemBean.ItemDetailsBean>(bean.getmCurUseProduct()) {
            @Override
            public View getView(FlowLayout parent, int position, AddLogItemBean.ItemDetailsBean s) {
                TextView ck = (TextView) LayoutInflater.from(activity).inflate(R.layout.view_tag_flow,
                        holder.mTvNeed, false);
                ck.setText(s.getProductName());
                if (TextUtils.isEmpty(s.getProductName())) {
                    ck.setVisibility(View.GONE);
                }
                return ck;
            }
        });

        holder.mTvReduceItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });

        holder.mUtilsProject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurClickPosition = position;
                activity.startActivityForResult(new Intent(activity, AddProjectActivity.class), MyState.requestOne);
            }
        });

        holder.mUtilsPerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurClickPosition = position;
                activity.startActivityForResult(new Intent(activity, AddServicePersonActivity.class), MyState.requestTwo);
            }
        });

        holder.mTvChangeNeed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurClickPosition = position;
                activity.startActivityForResult(new Intent(activity, AddProductActivity.class), MyState.requestThree);
            }
        });
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.tv_reduce_item)
        TextView mTvReduceItem;
        @BindView(R.id.utils_project)
        MyItemTextView mUtilsProject;
        @BindView(R.id.utils_person)
        MyItemTextView mUtilsPerson;
        @BindView(R.id.id_tag_flow_size)
        TagFlowLayout mTvNeed;
        @BindView(R.id.tv_change_need)
        TextView mTvChangeNeed;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
